package cz.muni.fi.pa165.business.daoimpl;

import cz.muni.fi.pa165.business.dao.ForestDAO;
import cz.muni.fi.pa165.business.entity.Forest;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Marek Tuhársky
 */
@Repository
public class ForestDAOImpl implements ForestDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public void createForest(Forest forest) {
        if (forest == null) {
            throw new IllegalArgumentException("parameter forest is null");
        }
        if (forest.getId() != null) {
            throw new IllegalArgumentException("forest is already in database [id:" + forest.getId() + "]");
        }
        if (forest.getName() == null || forest.getName().isEmpty()) {
            throw new IllegalArgumentException("parameter forest doesn't have attribute name filled");
        }
        em.persist(forest);
    }

    @Override
    public Forest getForest(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("parameter id is null");
        }
        if (id < 0) {
            throw new IllegalArgumentException("parameter id is negative");
        }
        return em.find(Forest.class, id);
    }

    @Override
    public void updateForest(Forest forest) {
        if (forest == null) {
            throw new IllegalArgumentException("parameter forest is null");
        }
        if (forest.getName() == null || forest.getName().isEmpty()) {
            throw new IllegalArgumentException("parameter forest doesn't have attribute name filled");
        }
        if (forest.getId() == null) {
            throw new IllegalArgumentException("given forest is not in DB");
        }
        em.merge(forest);
    }

    @Override
    public void removeForest(Forest forest) {
        if (forest == null) {
            throw new IllegalArgumentException("parameter forest is null");
        }
        if (forest.getId() == null) {
            throw new IllegalArgumentException("given forest isnt in DB");
        }
        Forest f = em.find(Forest.class, forest.getId());
        em.remove(f);
    }

    @Override
    public List<Forest> retrieveAllForests() {
        try {
            Query queryFindAll = em.createQuery("SELECT f FROM "
                    + "cz.muni.fi.pa165.business.entity.Forest f");
            return queryFindAll.getResultList();
        } catch (NoResultException ex) {
            return null;
        }
    }

    @Override
    public Forest retrieveForestByName(String name) {
        if (name == null || name.isEmpty()) {
            throw new IllegalArgumentException("parameter name is not given");
        }
        try {
            Query queryFindByName = em.createQuery("SELECT f FROM "
                    + "cz.muni.fi.pa165.business.entity.Forest f WHERE f.name = :name")
                    .setParameter("name", name);
            return (Forest) queryFindByName.getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }
    }
}
